#include<stdio.h>

#define N 100010

typedef struct PII{
    int left;
    int right;
}PII;

int n;
PII a[N];

int max(int a, int b){ return a > b ? a : b; }

void quick_sort(PII a[], int l, int r){
    if( l >= r ) return;
    int x = a[(l+r)/2].left, i = l - 1, j = r + 1;
    while(i < j){
        do i++;  while(a[i].left < x);
        do j--;  while(a[j].left > x);
        if(i < j){
            PII temp = a[i];
            a[i] = a[j];
            a[j] = temp; 
        }
    }
    quick_sort(a, l, j);
    quick_sort(a, j+1, r);
}

int merge(PII a[]){

    int count = 0;
    int st = -2e9, ed = -2e9;
    for(int i = 0; i < n; i++){
        if(ed < a[i].left){
            if(st != -2e9)  count++;
            st = a[i].left, ed = a[i].right;
        }
        else  ed = max(ed, a[i].right);
    }

    if(st != -2e9)  count++;   //当只有一个区间时候更新了st = a[i].left 但是却没count++  因此再后面加上

    return count;
}

int main(){

    scanf("%d", &n);

    for(int i = 0; i < n; i++)   scanf("%d%d", &a[i].left, &a[i].right);

    quick_sort(a, 0, n-1);        //对a排序

    printf("%d", merge(a));

    return 0;
}

***************************************************************************************************************


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e5 + 10;
int n;

void merge(vector<PII> &a)
{
    vector<PII> res;
    
    sort(a.begin(), a.end());
    
    int st = -2e9, ed = -2e9;
    for(auto tmp : a)
    {
        if(ed < tmp.first)
        {
            if(ed != -2e9) res.push_back({st, ed});
            st = tmp.first, ed = tmp.second;
        }
        else ed = max(ed, tmp.second);
    }
    
    if(st!= -2e9) res.push_back({st, ed});
    
    a = res;
}

int main()
{
    vector<PII> a;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        a.push_back({l, r});
    }
    
    merge(a);
    
    cout << a.size() << endl;
    return 0;
}